﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Security.AccessControl;

namespace XBMC.Cache.Manager.Shared
{
    public static class RegistryHelper
    {
        private static string m_RegistryRoot = @"SOFTWARE\XBMC Cache Manager";

        public static void SaveRegistryOption(string valueName, string value)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(m_RegistryRoot,
                RegistryKeyPermissionCheck.ReadWriteSubTree,
                RegistryRights.ReadKey | RegistryRights.SetValue);

            if (key == null) key = Registry.LocalMachine.CreateSubKey(m_RegistryRoot);

            if (value == null)
            {
                try { key.DeleteValue(valueName); }
                catch { }
            }
            else
            {
                key.SetValue(valueName, value);
            }

            key.Close();
        }

        public static string LoadRegistryOption(string valueName)
        {
            string value = null;

            RegistryKey key = Registry.LocalMachine.OpenSubKey(m_RegistryRoot,
                RegistryKeyPermissionCheck.ReadWriteSubTree,
                RegistryRights.ReadKey | RegistryRights.SetValue);

            try { value = key.GetValue(valueName).ToString(); }
            catch { }

            try { key.Close(); }
            catch { }

            return value;
        }
    }
}
